package com.jrzh.mvc.controller.sys.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.NoPermission;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.search.sys.RoleSearch;
import com.jrzh.mvc.service.sys.ResourcesServiceI;
import com.jrzh.mvc.service.sys.RoleServiceI;
import com.jrzh.mvc.view.sys.RoleView;

@RestController(RoleController.LOCATION +"/RoleController")
@RequestMapping(RoleController.LOCATION)
public class RoleController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/role";
	
	@Resource(name = "roleService")
	private RoleServiceI roleService;
	
	@Resource(name = "resourcesService")
	private ResourcesServiceI resourcesService;
	
	@PostMapping(value = "datagrid")
	@UserEvent(desc = "角色列表查询")
	@RequiredPermission(key = SysActionConstants.INDEX)
	public JqDataGrid<RoleView> datagrid(RoleSearch search) {
		JqDataGrid<RoleView> dg = new JqDataGrid<RoleView>();
		try {
			search.setSort("sortNum");
			dg = roleService.datagrid(search);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dg;
	}
	
	
	@PostMapping(value = "add")
	@UserEvent(desc = "角色增加")
	@RequiredPermission(key = SysActionConstants.ADD)
	public ResultBean add(RoleView view) {
		ResultBean result = new ResultBean();
		try {
			roleService.addByView(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		}catch (Exception e) {
			log.info(e);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "edit/{id}")
	@RequiredPermission(key = SysActionConstants.EDIT)
	@UserEvent(desc = "角色修改")
	public ResultBean edit(@PathVariable("id") String id, RoleView view) {
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			roleService.editByView(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.modify_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "changeStatus/{id}")
	@RequiredPermission(key = SysActionConstants.DISABLE_ENABLE)
	@UserEvent(desc = "改变角色禁用/启用状态")
	public ResultBean changeStatus(@PathVariable("id") String id, RoleView view){
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			roleService.editByFields(view, new String[]{"isDisable"}, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "setResources/{id}")
	@UserEvent(desc = "角色授权")
	@RequiredPermission(key = SysActionConstants.AUTHORIZATION)
	public ResultBean setResources(@PathVariable("id") String id, RoleView view) {
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			roleService.saveResources(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.authorization_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "loadResources/{id}")
	@UserEvent(desc = "查询角色授权")
	@NoPermission
	public ResultBean loadResources(@PathVariable("id") String id) {
		ResultBean result = new ResultBean();
		try {
			result.setObjs(new Object[]{roleService.loadResources(id)});
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.authorization_success", getSessionUser()));
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "delete/{id}")
	@RequiredPermission(key = SysActionConstants.DELETE)
	@UserEvent(desc = "角色删除")
	public ResultBean delete(@PathVariable("id") String id) {
		ResultBean result = new ResultBean();
		try {
			roleService.delete(id, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.delete_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	/**
	 * 获取角色列表
	 * @param search
	 * @return
	 */
	@PostMapping(value = "getAllRoles")
	@NoPermission
	public ResultBean getAllRoles(){
		ResultBean result = new ResultBean();
		List<RoleView> roleList = new ArrayList<RoleView>();
		try {
			roleList = roleService.selectViewByField("isDisable", false);
			result.setStatus(ResultBean.SUCCESS);
			result.setObjs(new Object[]{roleList});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@PostMapping(value = "loadData/{id}")
	@RequiredPermission(key = SysActionConstants.VIEW)
	public ResultBean loadData(@PathVariable("id") String id, RoleView view) {
		ResultBean result = new ResultBean();
		try {
			result.setObjs(new Object[]{roleService.findViewById(id)}); 
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.inquire_success", getSessionUser()));
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Override
	protected void setData() {
		
	}
}